package com.dou361.ijkplayer.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 本类为自定义的viewgroup展示heartview（点赞按钮）
 */

public class HeartViewGroup extends FrameLayout implements View.OnTouchListener {
  private Context mContext;
  private CompleteAnimListener listener;
  private long mCurTime;
  private long mLastTime;
  //创建软引用集合
  private WeakReference<HeartImageView> weak;
  private List<WeakReference<HeartImageView>>list;
  private Random mRandom;

  public HeartViewGroup(Context context) {
    this(context, null);
  }

  public HeartViewGroup(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public HeartViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    list=new ArrayList<>();
    mContext = context;
    mRandom = new Random();

    setOnTouchListener(this);
  }
  private Handler mHandler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);

    }
  };
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        mLastTime=mCurTime;
        mCurTime= System.currentTimeMillis();
        if(mCurTime-mLastTime<200) {//双击事件
          Log.d("zzz",event.getRawX()+"----"+event.getRawY());
          final HeartImageView mImageHeart = new HeartImageView(mContext);
          weak=new WeakReference<HeartImageView>(mImageHeart);
          FrameLayout.LayoutParams rl =
                  new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                          ViewGroup.LayoutParams.WRAP_CONTENT);
          mImageHeart.setLayoutParams(rl);
          int widthMeasureSpec = MeasureSpec.makeMeasureSpec(400, MeasureSpec.EXACTLY);
          int heightMeasureSpec = MeasureSpec.makeMeasureSpec(400, MeasureSpec.EXACTLY);
          mImageHeart.measure(widthMeasureSpec, heightMeasureSpec);
          int left = (int) event.getX() - mImageHeart.getMeasuredWidth() / 2;
          int top = (int) event.getY() - mImageHeart.getMeasuredHeight();
          int right = (int) event.getX() + mImageHeart.getMeasuredWidth() / 2;
          int bottom = (int) event.getY();
          FrameLayout.LayoutParams rl1 = (LayoutParams) mImageHeart.getLayoutParams();
          rl1.setMargins(left,top,right,bottom);
          mImageHeart.setLayout(left,top,right,bottom);
          addView(mImageHeart,rl);
          mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
              setAnim(mImageHeart);
            }
          },200);

          list.add(weak);
          return true;
        }else {
          return false;
        }


    }
    return true;
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    int childCount = getChildCount();
    for(int i=0;i<childCount;i++){
      if(getChildAt(i) instanceof HeartImageView){
        HeartImageView heartImageView = (HeartImageView) getChildAt(i);
        heartImageView.layout(heartImageView.getL(),heartImageView.getT(),heartImageView.getR(),heartImageView.getB());
        Log.d("zzz","layout"+i);
      }
    }
  }

  public void setAnim(final ImageView img) {
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", 1f, 1.5f, 2f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(img, "scaleY", 1f, 1.5f, 2f);
    ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", 1f, 0.5f, 0f);
      ObjectAnimator trans=ObjectAnimator.ofFloat(img,"translationY",1f,1.5f,2f);

    AnimatorSet set = new AnimatorSet();
    set.play(scaleX).with(scaleY).with(alpha).with(trans);
    set.setDuration(500);

    set.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animator) {

      }

      @Override
      public void onAnimationEnd(Animator animator) {
        removeView(img);
        list.remove(weak);
          listener.onAnimEnd();

      }

      @Override
      public void onAnimationCancel(Animator animator) {

      }

      @Override
      public void onAnimationRepeat(Animator animator) {

      }
    });
//    scaleX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                  @Override
//                  public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                      img.requestLayout();
//                      img.invalidate();
//                  }
//              });
//              scaleY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                  @Override
//                  public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                      img.requestLayout();
//                      img.invalidate();
//                  }
//              });
//              alpha.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                  @Override
//                  public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                      img.requestLayout();
//                      img.invalidate();
//                  }
//              });
        set.start();
  }
  public interface CompleteAnimListener{
      void onAnimEnd();
  }
  public void setListener(CompleteAnimListener listener)
  {
      this.listener=listener;
  }
}
